#include<bits/stdc++.h>
#define int long long
#define N 1010
using namespace std;
inline int read() {
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')
		x=x*10+ch-'0',ch=getchar();
	return x*f;
}
void write(int x) {
	if(x<0)
		putchar('-'),x=-x;
	if(x>9)
		write(x/10);
	putchar(x%10+'0');
	return;
}

int n, m, mod, Q, ans = 0;
int a[N][N], sum[N][N];
int Pow(int a, int n) {
	if(n == 0) return 1;
	if(n == 1) return a % mod;
	int x = Pow(a, n / 2);
	if(n % 2 == 0) return x * x % mod;
	else return x * x % mod * a % mod;
}
int inv(int x) {
	return Pow(x, mod - 2);
}
signed main() {
	freopen("tsuki.in", "r", stdin);
	freopen("tsuki.out", "w", stdout);
	n = read();
	m = read();
	mod = read();
	if(mod == 998244353) {
		for(int i = 0; i <= n; i++)
			for(int j = 0; j <= m; j++) {
				sum[i][j] = 1;
			}
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++) {
				a[i][j] = read();
				sum[i][j] = sum[i][j - 1] * sum[i - 1][j] % mod * inv(sum[i - 1][j - 1]) % mod * a[i][j] % mod;
			}
		Q = read();
		for(int i = 1; i <= Q; i++) {
			int x1, y1, x2, y2;
			x1 = read();
			y1 = read();
			x2 = read();
			y2 = read();
			int now = sum[x2][y2] * sum[x1 - 1][y1 - 1] % mod * inv(sum[x2][y1 - 1]) % mod * inv(sum[x1 - 1][y2]) % mod;
			ans += (i ^ now);
			ans %= 147744151;
		}
		write(ans);
		return 0;
	}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			a[i][j] = read();
	Q = read();
	for(int i = 1; i <= Q; i++) {
		int x1, y1, x2, y2;
		x1 = read();
		y1 = read();
		x2 = read();
		y2 = read();
		int now = 1;
		for(int i = x1; i <= x2; i++)
			for(int j = y1; j <= y2; j++)
				now = (now * a[i][j]) % mod;
		ans += (i ^ now);
		ans %= 147744151;
	}
	write(ans);
	return 0;
}
